<?php

namespace App\Http\Controllers\Mini;

use App\Http\Controllers\Controller;
use App\Models\ClassStudentModel;
use App\Models\StudentCourseModel;
use App\Models\StudentSignModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\StudentModel;
use App\Tools\SignTemplate;
use App\Models\ClassCourseModel;
use App\Models\CourseArrangeModel;
use App\Models\TeacherModel;
use App\Services\CourseService;

class TeacherCourseController extends Controller
{
    public function courses(Request $request){

        $courses = CourseArrangeModel::with(['course', 'class', 'room', 'teacher'])
        ->where('teacher_id', $request->teacherId)
        ->where('date', $request->date)
        ->orderBy('time')
        ->get();
        return self::ok($courses);
    }

    public function info(Request $request) {
        // 排课信息
        $course = CourseArrangeModel::with(['course', 'class', 'room', 'teacher'])
        ->where('id', $request->id)
        ->first();
        

        // 学员列表
        $students = DB::table('class_student as cs')
        ->leftJoin('student as s', 's.id', '=', 'cs.student_id')
        ->select('cs.*', 's.name as student_name')
        ->where('cs.class_id', $course->class_id)
        ->where('cs.course_id', $course->course_id)
        ->where('cs.deleted_at', null)
        ->where('s.deleted_at', null)
        ->get();
        
        return self::ok([
            'course' => $course,
            'students' => $students
        ]);
    }

    private const SIGN_RESULT = [ '未签到', '正常', '迟到', '请假', '旷课' ];
    
    public function sign(Request $request) {
        $studentId = $request->input('student_id');
        $arrangeCourseId = $request->input('arrange_course_id');
        $courseId = $request->input('course_id');
        $result = $request->input('result');
        $courseNum = $request->input('course_num');
        $res = CourseService::sign($arrangeCourseId, $courseId, $studentId, $result, $courseNum, false);        
        if($res === true) {
            return self::ok();
        } else {
            return self::err($res);
        }
    }

    public function signList(Request $request) {
        // $filter = $request->only(['course_id', 'class_id', 'date', 'time']);
        $signList = StudentSignModel::where(['arrange_id' => $request->arrangeId])->get();
        return self::ok($signList);
    }
}
